package com.dundun.SSO.core.page;


import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**********************************************************
 * 版权所有：搜影科技 保留所有权利
 * 创建日期: 2017/3/21 0021 17:06
 * 创建作者: gezhangkai
 * 版    本:
 * 功    能:
 * 最后修改时间:
 * 修改记录:
 ***********************************************************/
public class PageableSupport implements Serializable{
    public static final int DEFAULT_PAGE_SIZE = 20;
    public static final int DEFAULT_PAGE_NUM = 1;
    public static final String DEFAULT_ORDER = "desc";
    public static final String DEFAULT_SORT = "id";

    private int page = DEFAULT_PAGE_SIZE;
    private int rows = DEFAULT_PAGE_NUM;

    private String order = DEFAULT_ORDER ;
    private String sort = DEFAULT_SORT ;

    private int pageOffset;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page <= 0 ? DEFAULT_PAGE_SIZE : page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows <= 0 ? DEFAULT_PAGE_NUM : rows;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        if (StringUtils.isBlank(sort)) {
            sort = "id";
        }
        String sortTemp = sort.toLowerCase();
        for (int i = sort.length() - 1; i >= 0; i--) {
            if (!sort.substring(i, i + 1).equals(sortTemp.substring(i, i + 1))) {
                sortTemp = sortTemp.substring(0, i) + "_" + sortTemp.substring(i);
            }
        }
        this.sort = sortTemp;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        if(StringUtils.isNotBlank(order) && order.equalsIgnoreCase("asc")) {
            this.order = order;
        }
    }

    public int getPageOffset() {
        return pageOffset;
    }

    public void setPageOffset(int pageOffset) {
        this.pageOffset = pageOffset;
    }
}
